<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :treeoode="companyInfo" @on-add="showAddDepDialog" />
        <!-- 树形组件 -->
        <el-tree :data="treeData">
          <TreeTools slot-scope="{data}" :treeoode="data" @on-delete="loadDepartment" @on-add="showAddDepDialog" />
        </el-tree>
      </el-card>
      <!-- 新增部门的弹层 -->
      <DeptsDialog :deptsdata="node" :dialogvisible.sync="addDepDialogVisible" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import DeptsDialog from './components/depts-dialog.vue'
import { getDepartmentList } from '@/api/department.js'
import { listToTree } from '@/utils'
export default {
  name: 'Departments',
  filters: {},
  components: {
    TreeTools,
    DeptsDialog
  },
  props: {},
  data() {
    return {
      // 树形结构数据
      treeData: [],
      // 公司信息
      companyInfo: {
        name: '',
        manager: '负责人',
        isDone: false
      },
      // 控制弹层状态
      addDepDialogVisible: false,
      // 子组件传递的节点数据
      node: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadDepartment()
  },
  methods: {
    // 获取组织架构数据
    async loadDepartment() {
      const res = await getDepartmentList()
      // console.log(res)
      this.companyInfo.name = res.companyName
      this.treeData = listToTree(res.depts, '')
      this.companyInfo.children = this.treeData
      // console.log(this.treeData)
    },
    showAddDepDialog(data) {
      this.addDepDialogVisible = true
      this.node = data
    }
  }
}
</script>

<style scoped lang='scss'>
.tree-card {
  // padding: 30px  140px;
  font-size:14px;
}
.dashboard-container{
  .el-row{
    width: 100%;
  }
}
</style>
